package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.ejb.EJB;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ejb.AccountBeanLocal;
import ejb.LoginBeanLocal;
import entities.Client;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@EJB
	LoginBeanLocal lb;
	@EJB
	AccountBeanLocal account;
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession(true);
		String username = request.getParameter("login");
		String password = request.getParameter("password");
		Client cl = lb.getUser(username);
		if (cl!=null && cl.getPassword().equals(password)) {
			account.initialize(cl);
			session.setAttribute("Username", username);
		}
		else {
			session.invalidate();
		}
		response.sendRedirect("/NCProjectWeb/index.jsp");
	}
}
